<?php

namespace app\common\model;

use think\Model;

class Article extends Model
{
    // 数据表名称
    //**不论什么时候 加上表前缀**
    protected $table = 'article';
    // 数据表主键 复合主键使用数组定义 不设置则自动获取
    protected $pk = 'aid';

    /**
     * 手册搜索  数据完成
     */
    //添加和修改时都会处理的字段
    protected $auto = ['user_uid'];
    //添加时处理的字段
    protected $insert = ['sendtime'];
    //修改时处理的字段
    protected $update = ['updatetime'];
    protected function setUserUidAttr(){
        return session('admin.uid');
    }
    protected function setSendTimeAttr(){
        return time();
    }
    protected function setUpdateTimeAttr(){
        return time();
    }
    public function add($data){
        //1.执行文章表的验证及添加
        //过滤掉post数组中的非文章数据表字段数据
        $result = $this->validate(true)->allowField(true)->save($data);
        if(false === $result){
            //验证失败  输出错误信息
            return ['valid'=>0,'msg'=>$this->getError()];
        }else{
            //2.执行文章标签中间表的添加
            foreach ($data['tag_tid'] as $v){
                $tagData = [
                    //获取自增的id
                    'article_aid'=>$this->aid,
                    'tag_tid'=>$v
                ];
                (new ArcTag())->save($tagData);
            }
            //3.执行文章数据表的添加
            $arcData = [
                'keywords'=>$data['keywords'],
                'des'     =>$data['des'],
                'content' =>$data['content'],
                'article_aid'=>$this->aid,
            ];
            (new ArcData())->save($arcData);
            return ['valid'=>1,'msg'=>'添加成功'];
        }
    }

    public function recycle($aid){
        //将文章表中的is_recycle改为1
        $bool = $this->save(['is_recycle'=>1],['aid'=>$aid]);
        if($bool){
            return ['valid'=>1];
        }else{
            return ['valid'=>0];
        }
    }

    //编辑
    public function edit($data){
        $aid = input('param.aid');
        //1.执行文章表的验证及编辑
        //过滤掉post数组中非文章表中的字段
        $result = $this->validate(true)->allowField(true)->save($data,[$this->pk=>$aid]);
        if(false === $result){
            //验证失败  输出错误信息
            return ['valid'=>0,'msg'=>$this->getError()];
        }else{
            //2.更新文章数据表
            $arcData = [
                'keywords'=>$data['keywords'],
                'des'     =>$data['des'],
                'content' =>$data['content'],
            ];
            $bool =db('article_data')->where('article_aid',$aid)->update($arcData);
            if(!$bool){
                return ['valid'=>0,'msg'=>'文章数据表更新失败'];
            }

            //3.更新文章标签中间表(先删除在添加)
            $row =db('article_tag')->where('article_aid',$aid)->delete();
            if($row){
                //执行文章标签中间表的添加
                foreach ($data['tag_tid'] as $v){
                    $tagData = [
                        'article_aid'=>$aid,
                        'tag_tid'=>$v
                    ];
                    (new ArcTag())->save($tagData);
                }

            }else{
                return ['valid'=>0,'msg'=>'文章标签中间表删除失败'];
            }
            return ['valid'=>1,'msg'=>'编辑成功'];
        }
    }

}
